import random
from Response import Response
import FeedBack

class Text:
    def __init__(self,text,color=None):
	self.text=text

class Action:
    def __init__(self,action,value):
	self.action=action;
	self.value=value
    def __str__(self):
	return "    type=[" + self.action.encode("utf-8") + "] value=[" + str(self.value) + "]\n"


class Tile:
    def __init__(self):
	self.condGroup = None
	self.action = None
	self.text = None
        self.x = None
        self.y = None
        self.color = None

    def setCond(self, condGroup):
	self.condGroup=condGroup

    def setAction(self, action,value=0):
	self.action=Action(action,value)

    def setText(self, text):
	self.text=text

    def __str__(self):
	ret= ''
        ret += '  color=[' + self.color.encode('utf-8') + ']\n'
        ret += '  x=[' + self.x.encode('utf-8') + ']\n'
        ret += '  y=[' + self.y.encode('utf-8') + ']\n'
	if(self.text != None):
	    ret += '  text=[' + self.text.encode("utf-8") + "]\n"
	if(self.condGroup != None):
	    ret += '  Condgroup=[' + self.condGroup.encode("utf-8") + "]\n" 
	if(self.action != None):
	    ret += '  Action:\n' + str(self.action)
	return ret

class Quizzes:
    def __init__(self, groupId):
	self.quizzes=[]
	self.used=[]

    def Get(self):
	if (len(self.quizzes) == 0):
	    self.quizzes = self.used
	    self.used = []
	quiz = self.quizzes.pop();
	self.used.append(quiz)
	return quiz
	

    def __str__(self):
	ret = '  New:\n'
	for quiz in self.quizzes:
	    ret += str(quiz)
	ret += '  Used:\n'
	for quiz in self.used:
	    ret += str(quiz)
	return ret
    

class Quiz:
    def __init__(self, question, right, all):
	self.question = question
	self.answer = right
	self.options = all

    def get(self):
	pass

    def isRight(marked):
	pass

    def __str__(self):
	ret = '    Question=['+self.question.encode("utf-8")+"]\n"	
	ret += '    Right=['+self.answer.encode("utf-8")+"]\n"
	for a in self.options:    
	    ret += '      Choice=['+a.encode("utf-8")+"]\n"
	return ret

class Board:
    def __init__(self):
	#indexado por id
	self.tiles={}

	#indexado por group name
	self.groups={}

	#indexado por current
	self.path = {}

	self.pathback = {}
	
	# a posicao de cada jogador, indexado pelo id
	self.positions = {}

	self.last = {}

	self.quiz = {}

	self.won = ''

    def __str__(self):
	ret = ''
	for key in self.tiles.iterkeys():
	    ret += "TILEID=[" + key.encode("utf-8")  +']\n' + str(self.tiles[key])
	for key in self.groups.iterkeys():
	    ret += "GROUPID=[" + key.encode("utf-8")  +']\n' + str(self.groups[key])
	ret += "Path: "+str(self.path)

	return ret	

    def AddPlayer(self, player):
	print "Player ",player, " added to the game."
	self.positions[player] =self.path["BEGIN"]

    def PostProcess(self):
	for key in self.path.iterkeys():
	    self.pathback[self.path[key]]=key

    def Move(self,player,moves):
	current = self.positions[player]
	self.last[player]=current
#	print 'player ', player, 'current position ', current
	if(moves>=0):
	    for i in range(moves):
		current = self.path[current]
		if (current == 'END'):
		    self.won = player
		    print player, 'won'
		    return None
	else:
	    for i in range(-moves):
		if (current == 'BEGIN'):
		    current = self.path['BEGIN']
		    break
		current = self.pathback[current]

#	print 'player ', player, ' final position', current
	self.positions[player]=current

	tile = self.tiles[current]
	quiz = None
	if (tile.condGroup!=None):
	    quiz =  self.groups[tile.condGroup].Get()
	    self.quiz[player]=quiz
	return FeedBack.Action(current, quiz)

    def Act(self, player, response):
	tile = self.tiles[self.positions[player]]
	if(self.won == player):
	    return Response(player, self.positions[player], 'WIN');
	if(self.quiz.has_key(player)):
	    quiz = self.quiz[player]
	    self.quiz.pop(player)
	    if ( quiz.answer == response):
		print player, " got it!"
		if(tile.action.action == 'FOWARD'):
		    self.Move(player,tile.action.value);
		elif(tile.action.action == 'AGAIN'):
		    return Response(player, self.positions[player], "AGAIN")
		return Response(player, self.positions[player], "OK")
	    else:
		if(tile.action.action == 'BACK'):
		    self.Move(player,-tile.action.value);
		return Response(player, self.positions[player], "MISS")
	return Response()

